//
//  DXWGoddnessHTMLViewController.m
//  pandaTV
//
//  Created by 淡馨慰 on 16/10/14.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWGoddnessHTMLViewController.h"
#import "UIBarButtonItem+DXWBarButtonItem.h"

@interface DXWGoddnessHTMLViewController ()<UIWebViewDelegate>

/** 网页 */
@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, assign) NSInteger clickNumber;

/** 左侧返回 */
@property (nonatomic, strong) UIButton *backBtn;

/** <#name#> */
@property (nonatomic, strong)  NSString *currentHTML;


/** <#name#> */
@property (nonatomic, strong)  NSString *currentURL;
@end

@implementation DXWGoddnessHTMLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      [self setupWebView];


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageWithOriginalImageName:@"setting_back"] forState:UIControlStateNormal];
    //    [btn setImage:higtLightedImage forState:UIControlStateHighlighted];
    [btn sizeToFit];
    [btn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //保证点击的范围在图片之中
    UIView *btnView = [[UIView alloc]initWithFrame:btn.bounds];
    [btnView addSubview:btn];
    _backBtn = btn;
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btnView];
    self.navigationItem.leftBarButtonItem = item;


    NSLog(@"%@",_currentURL);
    NSLog(@"%@",_currentHTML);
    
}

//获取当前页面的title和url
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
//    [SVProgressHUD dismiss];
    NSString * title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title
    self.navigationItem.title = title;
    //获取当前网页的html
    self.currentURL = webView.request.URL.absoluteString;
    NSLog(@"title-%@--url-%@--",self.title,self.currentURL);
    NSString *lJs = @"document.documentElement.innerHTML";
    self.currentHTML = [webView stringByEvaluatingJavaScriptFromString:lJs];
}
- (void)setupWebView
{
    UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    view.backgroundColor = dxw_MainColor;
    
    UIWebView *webView = [[UIWebView alloc]initWithFrame:view.bounds];
    webView.delegate = self;
    [view addSubview:webView];
    _webView = webView;
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://m.panda.tv/activity/godvoteios.html"]];
    _clickNumber = 0;
    [webView loadRequest:request];
    
}
#pragma mark -UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//    //判断是否是单击
//    if (navigationType == UIWebViewNavigationTypeLinkClicked)
//    {
//        
//
//        
//         return YES;
//    }
 
//    if ([_webView canGoBack]) {
//        _backBtn.hidden = NO;
//        [_webView goBack];
//    }
//    else
//        _backBtn.hidden = YES;

    return YES;
   
}


- (void)backBtnClick
{
    if ([_webView canGoBack]) {
//        _backBtn.hidden = NO;
        [_webView goBack];
    }
//    else
//        _backBtn.hidden = YES;

}


@end
